﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DBUtils;

public partial class EmployeeInfo_SearchEmp : System.Web.UI.Page
{
    DataSet dsResult;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["dept"] != null)//如果当前是查询某一部门所有员工
        {
            //预先载入GridView的内容，以便分页
            dsResult = EmployeeInfo.SearchEmpByDept(Request.QueryString["dept"].ToString());
            GridView1.DataSource = dsResult.Tables[0];
            Session["dsResult"] = dsResult;
            GridView1.DataBind();
        }
        ////else//如果当前是查询某一部门所有员工
        ////{
        ////    dt = EmployeeInfo.SearchEmpByDept(Request.QueryString["dept"].ToString()).Tables[0];
        ////    GridView1.DataSource = dt;
        ////    GridView1.DataBind();
        ////    GridView1.Visible = true;
        ////}
        //if (IsPostBack)
        //{
        //    dsResult = (DataSet)Session["dsResult"];
        //}
    }

    /// <summary>
    /// 员工信息查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        dsResult=EmployeeInfo.SearchEmpByKey(txtKey.Text.Trim());

        if (dsResult.Tables[0].Rows.Count == 0)//如果没能查询到纪录则显示提示
        {
            lblNoResult.Text = "没有您要查询的结果";
            lblNoResult.Visible = true;
            return;
        }
        else if (dsResult.Tables[0].Rows.Count == 1)//若仅查询到一条信息则转向详细信息页面
        {
            Response.Redirect("DefaultInfo.aspx?id=" + dsResult.Tables[0].Rows[0].ItemArray[0].ToString());
            return;
        }
        else//查询到多条信息则显示在列表中
        {
            GridView1.DataSource = dsResult;
            Session["dsResult"] = dsResult;
            GridView1.DataBind();
            GridView1.Visible = true;
        }
    }

    /// <summary>
    /// 分页过程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        dsResult = (DataSet)Session["dsResult"];
        GridView1.DataSource = dsResult.Tables[0];
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dsResult = (DataSet)Session["dsResult"];
        if (e.CommandName == "LoginName")//点击“登陆名”列则转到相应的详细信息页面
        {
            string id = dsResult.Tables[0].Rows[Convert.ToInt32(e.CommandArgument)].ItemArray[0].ToString();
            Response.Redirect("DefaultInfo.aspx?id=" + dsResult.Tables[0].Rows[Convert.ToInt32(e.CommandArgument)].ItemArray[0].ToString());
        }

        if (e.CommandName == "DeptName")//点击“部门”则查询处该部门所有的员工
        {
            Response.Redirect("SearchEmp.aspx?dept=" + dsResult.Tables[0].Rows[Convert.ToInt32(e.CommandArgument)].ItemArray[3].ToString());
        }

        if (e.CommandName == "Email")//点击“Email”则开启邮件客户端程序
        {
            Response.Redirect("mailto:" + dsResult.Tables[0].Rows[Convert.ToInt32(e.CommandArgument)].ItemArray[4].ToString());
        }

    }
}
